<?php
/**
 * 服务商的token
 * 以corpid、provider_secret（获取方法为：登录服务商管理后台->标准应用服务->通用开发参数，可以看到）
 * 换取provider_access_token，代表的是服务商的身份，而与应用无关。
 * 请求单点登录、注册定制化等接口需要用到该凭证。
 */

namespace app\index\controller;


class Provider extends Common
{
    //通用开发参数接受
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    public $CorpID = 'wwda3795505c7a64de'; //CorpID

    public $ProviderSecret = 'CRK-pVzEqN-4HVIvGo9fJbqqkJfLMUDTxDZs5kXGU2aWzwF9VGVorIpiBRLW0R8N'; //ProviderSecret

    public $Token = 'pi8cqf'; //Token

    public $EncodingAESKey = 'vQEJeUAFcxO7ICh6B993UeWh17s5VVrOXfnnphPO7Iu'; //EncodingAESKey


    public function index()
    {
        if($xml = file_get_contents('php://input')){
            $this->ticket_accept($xml);
            die;
        }else{
            $this->url_accept();
        }

    }

    /*
  * ticket刷新
  * @param xml $xml xml文件
  * */
    public function ticket_accept($xml)
    {
        include_once '../vendor/suite/WXBizMsgCrypt.php';
        $EncryptMsg = simplexml_load_string($xml,'SimpleXMLElement',LIBXML_NOCDATA);
        $SuiteId = $EncryptMsg->SuiteId;
        $Encrypt = $EncryptMsg->Encrypt;
        $input = [
            'msg_signature' =>'',
            'timestamp' =>  '',
            'nonce' =>  '',
        ];
        foreach($input as $key => $value) {
            $tmp = urldecode($_REQUEST[$key]);
            if (!empty($tmp)) {
                $input[$key] = $tmp;
            }
        }
        $pc = new \WXBizMsgCrypt($this->Token,$this->EncodingAESKey,$this->CorpID);
        $msg = '';
        $errCode = $pc->DecryptMsg($input['msg_signature'],$input['timestamp'],$input['nonce'],$xml,$msg);
        if($errCode == 0){
            $msgXml = simplexml_load_string($msg,'SimpleXMLElement',LIBXML_NOCDATA);
            //suite_ticket保存
            if($msgXml->InfoType == 'suite_ticket'){
                file_put_contents('provider_ticket.log',$msgXml->SuiteTicket);
            }
            echo 'success';
        }
        file_put_contents('../runtime/error/provider.log',date('Y-m-d H:i:s') .'ticket保存错误 error：'.$errCode."\r\n", FILE_APPEND );

    }

    public function url_accept()
    {

        include_once '../vendor/suite/WXBizMsgCrypt.php';
        $input = [
            'msg_signature' =>'',
            'timestamp' =>  '',
            'nonce' =>  '',
            'echostr'   =>  '',
        ];
        foreach($input as $key => $value) {
            $tmp = Urldecode($_REQUEST[$key]);
            if (!empty($tmp)) {
                $input[$key] = $tmp;
            }
        }

        $pc = new \WXBizMsgCrypt($this->Token,$this->EncodingAESKey,$this->CorpID);
        $msg = '';
        $errCode = $pc->VerifyURL($input['msg_signature'],$input['timestamp'],$input['nonce'],$input['echostr'],$msg);
        file_put_contents('../runtime/error/provider.log',date('Y-m-d H:i:s') .'msg：'.json_encode($msg)."\r\n", FILE_APPEND );
        if($errCode==0){
            echo $msg."\n";
            die;
        }
        file_put_contents('../runtime/error/provider.log',date('Y-m-d H:i:s') .'errCode：'.$errCode."\r\n", FILE_APPEND );

    }

}